package com.rtx.edi.platform.express.auth.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rtx.edi.model.express.auth.dto.PlAuthShopAppDto;
import com.rtx.edi.model.express.auth.po.PlAuthApp;
import com.rtx.edi.model.express.auth.po.PlAuthShop;
import com.rtx.edi.model.express.auth.po.PlAuthShopApp;
import com.rtx.edi.platform.express.auth.mapper.PlAuthShopAppMapper;
import com.rtx.edi.platform.express.auth.service.PlAuthAppService;
import com.rtx.edi.platform.express.auth.service.PlAuthShopAppService;
import com.rtx.edi.platform.express.auth.service.PlAuthShopService;
import com.rtx.edi.platform.express.auth.service.TokenService;

import ch.qos.logback.core.joran.util.beans.BeanUtil;

@Service
public class PlAuthShopAppServiceImpl extends ServiceImpl<PlAuthShopAppMapper,PlAuthShopApp> implements PlAuthShopAppService {
	@Autowired
	private PlAuthShopService plAuthShopService;
	
	@Autowired
	private PlAuthAppService plAuthAppService;
	
	@Autowired
	private TokenService tokenService;
	
	Map<String,Map<String,Object>> TokenMap = new HashMap<String, Map<String,Object>>();
	
	@Override
	public void selectPlAuthShopApp(Page<PlAuthShopApp> pageParam, PlAuthShopApp shopapp) {
		LambdaQueryWrapper<PlAuthShopApp> query = Wrappers.lambdaQuery();
		query.eq(PlAuthShopApp::getDeleteflag, "0");
		if(shopapp!=null) {
			query.eq(shopapp.getShopid()!=null, PlAuthShopApp::getShopid, shopapp.getShopid())
			.eq(shopapp.getAppid()!=null, PlAuthShopApp::getAppid, shopapp.getAppid())
			.eq(shopapp.getActive()!=null, PlAuthShopApp::getActive, shopapp.getActive());
	
		}
		baseMapper.selectPage(pageParam,query);
		
	}

	@Override
	public Boolean addPlAuthShopApp(PlAuthShopApp shopapp) {
		return baseMapper.addPlAuthShopApp(shopapp)==1;
	}

	@Override
	public void delPlAuthShopApp(List<PlAuthShopAppDto> list) {
		for (PlAuthShopAppDto dto : list) {
			baseMapper.deleteById(dto.getId());
		}
		
	}

	@Override
	public List<HashMap<String,String>> buildUrlAuthShopapp(List<PlAuthShopAppDto> list) {
		List<HashMap<String,String>> resp=new ArrayList<HashMap<String,String>>(); 
		for (PlAuthShopAppDto dto : list) {
			PlAuthShop shop=plAuthShopService.getById(dto.getShopid());
			PlAuthApp app=plAuthAppService.getById(dto.getAppid());
			String type=app.getType();
			switch (type) {
			case "TB":
				resp.add(buildTaobaoAuthorize(dto,shop,app));
				break;
			case "DY":
				resp.add(buildDouyinAuthorize(dto,shop,app));
				break;
			case "PDD":
				resp.add(buildPddAuthorize(dto,shop,app));
				break;
			case "JD":
				resp.add(buildJdAuthorize(dto,shop,app));
				break;
			case "XHS":
				resp.add(buildXiaohongshuAuthorize(dto,shop,app));
				break;
			case "DW":
				resp.add(buildDewuAuthorize(dto,shop,app));
				break;
			case "WP":
				resp.add(buildWeiPingAuthorize(dto,shop,app));
				break;
			default:
				resp.add(buildOtherAuthorize(dto,shop,app));
				break;
			}
			
		}
		return resp;
		
	}
	
	/**
	 * 淘宝授权 -拼接授权url 
	 * @param shop
	 * @param app
	 */
	private HashMap<String,String> buildTaobaoAuthorize(PlAuthShopAppDto dto,PlAuthShop shop,PlAuthApp app) {
		HashMap<String,String> result=new HashMap<String,String>();
		// 1.1  拼接授权url 
		// https://oauth.taobao.com/authorize?response_type=code&client_id=11111111&redirect_uri=http://www.taobao.com&state=1212&view=web
		StringBuffer str=new StringBuffer("https://oauth.taobao.com/authorize");
		
		str.append("?response_type=code");
		str.append("&client_id="+app.getAppkey());
		str.append("&redirect_uri="+app.getRedirecturl());
		str.append("&state="+dto.getId());
		str.append("&view=web");
		String sendurl=str.toString();
		
		// 发送请求 
		
		result.put("authurl",sendurl);
		// 
	    return result;

		
	}
	
	/**
	 * 抖音授权
	 * @param shop
	 * @param app
	 */
	private HashMap<String,String> buildDouyinAuthorize(PlAuthShopAppDto dto,PlAuthShop shop,PlAuthApp app) {
		HashMap<String,String> result=new HashMap<String,String>();
		//https://fuwu.jinritemai.com/authorize?service_id=12321&state=dy123
		StringBuffer str=new StringBuffer("https://fuwu.jinritemai.com/authorize?");
		
		str.append("&service_id="+app.getAppkey());
		str.append("&state="+dto.getId()+"");
		String sendurl=str.toString();
		
		// 发送请求 
		
		result.put("authurl",sendurl);
		
		return result;
	}
	
	/**
	 * 拼多多授权
	 * @param shop
	 * @param app
	 */
	private HashMap<String,String> buildPddAuthorize(PlAuthShopAppDto dto,PlAuthShop shop,PlAuthApp app) {
		
		HashMap<String,String> result=new HashMap<String,String>();
		
		
		return result;
	}
	
	/**
	 * 京东授权
	 * @param shop
	 * @param app
	 */
	private HashMap<String,String> buildJdAuthorize(PlAuthShopAppDto dto,PlAuthShop shop,PlAuthApp app) {
		HashMap<String,String> result=new HashMap<String,String>();
		// 1.1  拼接授权url 
		// https://oauth.jdl.com/oauth/authorize?client_id=YOUR_APP_KEY &redirect_uri=YOUR_REDIRECT_URI&response_type=code
		StringBuffer str=new StringBuffer("https://oauth.jdl.com/oauth/authorize");
		
		str.append("?client_id="+app.getAppkey());
		str.append("&redirect_uri="+app.getRedirecturl());
		str.append("&state="+dto.getId());
		str.append("&view=web");
		String sendurl=str.toString();
		
		// 发送请求 
		
		result.put("authurl",sendurl);
		
		
		return result;
	}
	
	/**
	 * 小红书授权
	 * @param shop
	 * @param app
	 */
	private HashMap<String,String> buildXiaohongshuAuthorize(PlAuthShopAppDto dto,PlAuthShop shop,PlAuthApp app) {
		HashMap<String,String> result=new HashMap<String,String>();
		// 1.1  拼接授权url 
		// https://ark.xiaohongshu.com/ark/authorization?appId=xxx&redirectUri=xxx&state=xxx
		StringBuffer str=new StringBuffer("https://ark.xiaohongshu.com/ark/authorization");
		
		str.append("?response_type=code");
		str.append("&client_id="+app.getAppkey());
		str.append("&redirect_uri="+app.getRedirecturl());
		str.append("&state="+dto.getId());
		str.append("&view=web");
		String sendurl=str.toString();
		
		// 发送请求 
		
		result.put("authurl",sendurl);
		
		return result;
		
	}
	
	/**
	 * 得物授权
	 * @param shop
	 * @param app
	 */
	private HashMap<String,String> buildDewuAuthorize(PlAuthShopAppDto dto,PlAuthShop shop,PlAuthApp app) {
		HashMap<String,String> result=new HashMap<String,String>();
		
		return result;
		
	}
	
	/**
	 * 唯品会授权
	 * @param shop
	 * @param app
	 */
	private HashMap<String,String> buildWeiPingAuthorize(PlAuthShopAppDto dto,PlAuthShop shop,PlAuthApp app) {
		HashMap<String,String> result=new HashMap<String,String>();
		// 1.1  拼接授权url 
		// https://auth.vip.com/oauth2/authorize?client_id=67c99bb9&response_type=code&redirect_uri=http://iwms.spring56.com:8080/index.html
		StringBuffer str=new StringBuffer("https://auth.vip.com/oauth2/authorize");
		
		str.append("?response_type=code");
		str.append("&client_id="+app.getAppkey());
		str.append("&redirect_uri="+app.getRedirecturl());
		str.append("&state="+dto.getId());
		str.append("&view=web");
		String sendurl=str.toString();
		
		// 发送请求 
		
		result.put("authurl",sendurl);
		
		return result;
		
	}
	
	/**
	 * 其他授权
	 * @param shop
	 * @param app
	 */
	private HashMap<String,String> buildOtherAuthorize(PlAuthShopAppDto dto,PlAuthShop shop,PlAuthApp app) {
		HashMap<String,String> result=new HashMap<String,String>();
		
		
		return result;
	}

	@Override
	public void getTokenShopapp(List<PlAuthShopAppDto> list) throws Exception {
		
		for (PlAuthShopAppDto dto : list) {
			PlAuthShop shop=plAuthShopService.getById(dto.getShopid());
			PlAuthApp app=plAuthAppService.getById(dto.getAppid());
			String type=app.getType();
			switch (type) {
			case "TB":
				String token =tokenService.getTbtoken(app.getGetway(),app.getAppkey(),app.getAppsecret(),dto.getAuthcode());
				dto.setAccesstoken(token);
				PlAuthShopApp shopapp=new PlAuthShopApp();
				BeanUtils.copyProperties(dto,shopapp);
				this.updateById(shopapp);
				break;
			case "DY":
				
				
				break;
			case "PDD":
				
				break;
			case "JD":
				
				break;
			case "XHS":
				
				break;
			case "DW":
				
				break;
			default:
				
				break;
			}
			
		}
		
	}
	
}
